package com.sunwayworld.baseline.framework.deviceDocking;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;

import java.util.List;

public class MyDecoder extends MessageToMessageDecoder<ByteBuf> {

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
        out.add(appendPrettyHexDump(in));
    }

    private static String appendPrettyHexDump(ByteBuf buf) {
        StringBuilder dump = new StringBuilder();
        if(buf.readableBytes() > 0) {
            ByteBuf in = Unpooled.buffer(buf.readableBytes());
            buf.readBytes(in);
            for (int j = 0; j < in.capacity(); j++) {
                char unsignedByte = (char) in.getUnsignedByte(j);
                dump.append(unsignedByte);
            }
        }
        return dump.toString();
    }
}